﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Util;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Ability.Skill
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseSkillManager
    {
        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 给出脚本里面的可运动物体的数量(NPCs、Monster)
        /// </summary>
        public int Count
        {
            get { return m_Skills.Count; }
        }
        #endregion

        #region zh-CHS SkillManager方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, BaseSkill> m_Skills = new SafeDictionary<long, BaseSkill>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseTaxiNode"></param>
        public bool AddSkill( long iSkillSerial, BaseSkill baseSkill )
        {
            if ( baseSkill == null )
            {
                Debug.WriteLine( "BaseSkillManager.AddSkill(...) - baseSkill == null error!" );
                return false;
            }

            EventHandler<BeforeAddSkillCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeAddSkillCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeAddSkillCallEventArgs eventArgs = new BeforeAddSkillCallEventArgs( baseSkill, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Skills.Add( iSkillSerial, baseSkill );

            EventHandler<AfterAddSkillCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterAddSkillCall;
            if ( tempAfterEventArgs != null )
            {
                AfterAddSkillCallEventArgs eventArgs = new AfterAddSkillCallEventArgs( baseSkill, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool RemoveSkill( long iSkillSerial )
        {
            BaseSkill baseSkill = FindSkillOnSerial( iSkillSerial );
            if ( baseSkill == null )
                return false;

            EventHandler<BeforeRemoveSkillCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeRemoveSkillCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeRemoveSkillCallEventArgs eventArgs = new BeforeRemoveSkillCallEventArgs( baseSkill, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Skills.Remove( iSkillSerial );

            EventHandler<AfterRemoveSkillCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterRemoveSkillCall;
            if ( tempAfterEventArgs != null )
            {
                AfterRemoveSkillCallEventArgs eventArgs = new AfterRemoveSkillCallEventArgs( baseSkill, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public BaseSkill FindSkillOnSerial( long iSkillSerial )
        {
            return m_Skills.GetValue( iSkillSerial );
        }

        /// <summary>
        /// 
        /// </summary>
        [MultiThreadedWarningAttribute( "(Skill)当前的数组是列表临时产生的,不能保存数组用于以后操作:警告!" )]
        public BaseSkill[] ToArray()
        {
            return m_Skills.ToArray();
        }

        #endregion

        #region zh-CHS 共有事件 | en Public Event

        #region zh-CHS AddSkillCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeAddSkillCallEventArgs> m_ThreadEventBeforeAddSkillCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeAddSkillCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeAddSkillCallEventArgs> ThreadBeforeAddSkillCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddSkillCall );
                {
                    m_ThreadEventBeforeAddSkillCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddSkillCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddSkillCall );
                {
                    m_ThreadEventBeforeAddSkillCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddSkillCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterAddSkillCallEventArgs> m_ThreadEventAfterAddSkillCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterAddSkillCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterAddSkillCallEventArgs> ThreadAfterAddSkillCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddSkillCall );
                {
                    m_ThreadEventAfterAddSkillCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddSkillCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddSkillCall );
                {
                    m_ThreadEventAfterAddSkillCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddSkillCall );
            }
        }
        #endregion

        #region zh-CHS RemoveSkillCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeRemoveSkillCallEventArgs> m_ThreadEventBeforeRemoveSkillCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeRemoveSkillCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeRemoveSkillCallEventArgs> ThreadBeforeRemoveSkillCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveSkillCall );
                {
                    m_ThreadEventBeforeRemoveSkillCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveSkillCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveSkillCall );
                {
                    m_ThreadEventBeforeRemoveSkillCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveSkillCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterRemoveSkillCallEventArgs> m_ThreadEventAfterRemoveSkillCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterRemoveSkillCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterRemoveSkillCallEventArgs> ThreadAfterRemoveSkillCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveSkillCall );
                {
                    m_ThreadEventAfterRemoveSkillCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveSkillCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveSkillCall );
                {
                    m_ThreadEventAfterRemoveSkillCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveSkillCall );
            }
        }
        #endregion

        #endregion
    }
}
#endregion